package com.android.physics;

import android.graphics.Canvas;

class PhysicsObjectInStage<T extends IPhysicsObject> implements IPhysicsObject
{
    private final T mObject;
    private float mPosX;
    private float mPosY;
    private float mSpeedX;
    private float mSpeedY;
    private float mMaxSpeedX = Float.MAX_VALUE;
    private float mMaxSpeedY = Float.MAX_VALUE;
    
    //Life cycle tick
    private long mStartTick;
    private long mEndTick;
    
    
    public PhysicsObjectInStage(T object)
    {
        mObject = object;
    }

    public T getPhysicsObject()
    {
        return mObject;
    }

    public void setPos(float x, float y)
    {
        mPosX = x;
        mPosY = y;
    }

    public float getPosX()
    {
        return mPosX;
    }

    public float getPosY()
    {
        return mPosY;
    }
    
    public void setSpeedX(float speed)
    {
        mSpeedX = speed;
    }
    
    public float getSpeedX()
    {
        return mSpeedX;
    }
    
    public void addSpeedX(float deltSpeed)
    {
        mSpeedX += deltSpeed;
        if (mSpeedX > mMaxSpeedX)
        {
            mSpeedX = mMaxSpeedX;
        }
        else if (mSpeedX < -mMaxSpeedX)
        {
            mSpeedX = -mMaxSpeedX;
        }
    }
    
    public void setSpeedY(float speed)
    {
        mSpeedY = speed;
    }
    
    public float getSpeedY()
    {
        return mSpeedY;
    }
    
    public void addSpeedY(float deltSpeed)
    {
        mSpeedY += deltSpeed;
        if (mSpeedY > mMaxSpeedY)
        {
            mSpeedY = mMaxSpeedY;
        }
        else if (mSpeedY < -mMaxSpeedY)
        {
            mSpeedY = -mMaxSpeedY;
        }
    }
    
    public void setMaxSpeed(float speedX, float speedY)
    {
        mMaxSpeedX = speedX;
        mMaxSpeedY = speedY;
    }

    public void move()
    {
        mPosX += mSpeedX;
        mPosY += mSpeedY;
    }
    
    public void setLife(long startTick, long endTick)
    {
        mStartTick = startTick;
        mEndTick = endTick;
    }
    
    public long getStartTick()
    {
        return mStartTick;
    }
    
    public long getEndTick()
    {
        return mEndTick;
    }

    public void drawInStage(Canvas stageCanvas)
    {
        int saveCount = stageCanvas.save();
        stageCanvas.clipRect(mPosX, mPosY, mPosX + getWidth(), mPosY + getHeight());
        stageCanvas.translate(mPosX, mPosY);
        mObject.draw(stageCanvas);
        stageCanvas.restoreToCount(saveCount);
    }

    @Override
    public void setSize(int width, int height)
    {
        mObject.setSize(width, height);
    }

    @Override
    public int getWidth()
    {
        return mObject.getWidth();
    }

    @Override
    public int getHeight()
    {
        return mObject.getHeight();
    }

    @Override
    public void draw(Canvas canvas)
    {
        mObject.draw(canvas);
    }
}
